<?php
namespace app\index\controller;
use think\Controller;
use think\Session;
use think\Request;
use think\View;
use think\Db;
use app\index\validate\Valid;
use PHPExcel;
use PHPExcel_Writer_Excel5;
use PHPExcel_Style_Alignment;
use PHPExcel_Style_Fill;
use PHPExcel_Style_Border;

class Tifyyhxmxcx extends Common
{

	//列表--非常规和进场
    public function index()
    {

        $where = [];
        $sn = $this->request->get('hxmdbh');
        $this->assign('hxmdbh',$sn);
        $jxs_title = $this->request->get('jxsmc');
        $this->assign('jxsmc',$jxs_title);
        $wdmc = $this->request->get('wdmc');
        $this->assign('wdmc',$wdmc);
        $pp = $this->request->get('pp');
        $this->assign('pp',$pp);
        $sjfykm = $this->request->get('sjfykm');
        $this->assign('sjfykm',$sjfykm);
        $sqr = $this->request->get('sqr');
        $this->assign('sqr',$sqr);
        $start = $this->request->get('start');
        $this->assign('start',$start);
        $end = $this->request->get('end');
        $this->assign('end',$end);
        $zffs = $this->request->get('zffs');
        $this->assign('zffs',$zffs);

        if($sn){
            $where['t.sn'] = ['like',"%{$sn}%"];
        }
        if($start&&$end){
            $where['t.ctime'] = ['between',[strtotime($start),strtotime($end)]];
        }elseif($start){
            $where['t.ctime'] = ['>',strtotime($start)];
        }elseif($end){
            $where['t.ctime'] = ['<',strtotime($end)];
        }
        if($jxs_title){
            $jxs_ids = db('cg_jxs')->where('title','like',"%{$jxs_title}%")->column('id');
            $where['t.jxs_id'] = ['in',$jxs_ids];
        }
        if($wdmc){
            $net_ids = db('cg_net')->where('title','like',"%{$wdmc}%")->column('id');
            $where['t3.net_id'] = ['in',$net_ids];
        }
        if($pp){
            $brand_ids = db('pro_brands')->where('brand_name','like',"%{$pp}%")->column('id');
            $where['brand'] = ['in',$brand_ids];
        }
        if($sjfykm){
            $feetypeids = db('cg_feiyongkemu3')->where('title','like',"%{$sjfykm}%")->column('id');
            $where['t3.feetype'] = ['in',$feetypeids];
        }
        if($sqr){
            $sqr_ids = db('employee')->where('em_name','like',"%{$sqr}%")->column('id');
            $where['t.cuser'] = ['in',$sqr_ids];
        }
        if($zffs){
            $zffsids = db('cg_paymethod')->where('title','like',"%{$zffs}%")->column('id');
            $where['t3.paymethod'] = ['in',$zffsids];
        }

        $count = Db::table('cg_ti_hexiao t')
            ->join('cg_ti_hexiao_list t2','t2.hx_id=t.id')
            ->join('cg_ti_list t3','t3.id=t2.list_id')
            ->join('cg_ti t4','t4.id=t3.ti_id')
            ->where($where)
            ->count();
        $data = Db::table('cg_ti_hexiao t')
            ->join('cg_ti_hexiao_list t2','t2.hx_id=t.id')
            ->join('cg_ti_list t3','t3.id=t2.list_id')
            ->join('cg_ti t4','t4.id=t3.ti_id')
            ->order("t.id desc")
            ->where($where)
            ->field('t.cuser sqr,t.sn hxmdbh,t.id,t2.sn hxmxbh,t4.type_id,t4.sn ysqdh,t4.id ysqdh_id,t3.sn ysqmxbh,t.jxs_id,t3.net_id,t3.brand,t3.category,t3.series,t3.sku,t3.product,t3.tm_id,t3.feetype,t3.paymethod,t3.fb_shijitourufeiyong sjtrje,t3.hx_yihexiao2 yhxje,t2.weishuihexiaojine wshxje,t2.youwufapiao ywfp,t2.fapiaohao fph,t2.shizengzhishui szzs,t2.shuilv,t2.shuijin,t2.hanshuihexiaojine hshxje,t2.shijitourujine sjtrje,t2.huodongkaishiriqi hdksrq,t2.gdrq,t2.beizhu')
            ->paginate(10,$count);
//        echo Db::table('cg_ti')->getLastSql();
        $page = $data->render();
        $data = $data->items();
        $maxpage=ceil($count/10);
        foreach($data as $k=>$v){
            $v['sqr'] = db('employee')->where('id',$v['sqr'])->value('em_name');
            $v['type_title'] = db('cg_ti_type')->where('id',$v['type_id'])->value('title');
            $v['jxs_title'] = db('cg_jxs')->where('id',$v['jxs_id'])->value('title');
            $v['system_title'] = db('cg_system t')->join('cg_net t2','t2.system_id = t.id')->where('t2.id',$v['net_id'])->value('t.title');
            $v['net_title'] = db('cg_net')->where('id',$v['net_id'])->value('title');
            $v['brand_name'] = db('pro_brands')->where('id',$v['brand'])->value('brand_name');
            $v['category_name'] = db('pro_category')->where('id',$v['category'])->value('category_name');
            $v['series_name'] = db('pro_series')->where('id',$v['series'])->value('series_name');
            $v['sku_name'] = db('pro_sku')->where('id',$v['sku'])->value('sku_name');
            if($v['product'])
                $v['pro_name'] = db('pro_products')->where('id',$v['product'])->value('pro_name');
            else
                $v['pro_name'] = db('pro_products')->where('id',$v['tm_id'])->value('pro_name');
            $v['sjkm'] = db('cg_feiyongkemu3')->where('id',$v['feetype'])->value('title');
            $v['ejkm'] = db('cg_feiyongkemu2 t')->join('cg_feiyongkemu3 t2','t2.pid=t.id')->where('t2.id',$v['feetype'])->value('t.title');
            $v['yjkm'] = db('cg_feiyongkemu1 t')->join('cg_feiyongkemu2 t2','t2.pid=t.id')->join('cg_feiyongkemu3 t3','t3.pid=t2.id')->where('t3.id',$v['feetype'])->value('t.title');
            $v['zffs'] = db('cg_paymethod')->where('id',$v['paymethod'])->value('title');
            //反馈完成之后才能显示核销按钮
            $data[$k] = $v;
        }

        $this->assign('data',$data);
        $this->assign('page',$page);
		$this->assign('maxpage',$maxpage);
        //批复金额合计
        $pfjehj = Db::table('cg_ti_hexiao t')
            ->join('cg_ti_hexiao_list t2','t2.hx_id=t.id')
            ->join('cg_ti_list t3','t3.id=t2.list_id')
            ->join('cg_ti t4','t4.id=t3.ti_id')
            ->group("t3.id")
            ->field('t3.id,t3.pizhunhanshui')
            ->where($where)
            ->sum('t3.pizhunhanshui');
        $this->assign('pfjehj',$pfjehj);
        $wshxjehj = Db::table('cg_ti_hexiao t')
            ->join('cg_ti_hexiao_list t2','t2.hx_id=t.id')
            ->join('cg_ti_list t3','t3.id=t2.list_id')
            ->join('cg_ti t4','t4.id=t3.ti_id')
            ->group("t3.id")
            ->field('t3.id,t3.hx_yihexiao')
            ->where($where)
            ->sum('t3.hx_yihexiao');
        $this->assign('wshxjehj',$wshxjehj); //未税核销金额合计
        $hshxjehj = Db::table('cg_ti_hexiao t')
            ->join('cg_ti_hexiao_list t2','t2.hx_id=t.id')
            ->join('cg_ti_list t3','t3.id=t2.list_id')
            ->join('cg_ti t4','t4.id=t3.ti_id')
            ->group("t3.id")
            ->field('t3.id,t3.hx_yihexiao2')
            ->where($where)
            ->sum('t3.hx_yihexiao2');
        $this->assign('hshxjehj',$hshxjehj); //含税核销金额合计
        //税金合计
        $sjhj  = $hshxjehj-$wshxjehj;
        $this->assign('sjhj',$sjhj);
        //未核销金额合计
        $whxjehj = $pfjehj-$hshxjehj;
        $this->assign('whxjehj',$whxjehj);
        //实际核销金额合计
        $sjhxjehj = Db::table('cg_ti_hexiao t')
            ->join('cg_ti_hexiao_list t2','t2.hx_id=t.id')
            ->join('cg_ti_list t3','t3.id=t2.list_id')
            ->join('cg_ti t4','t4.id=t3.ti_id')
            ->group("t3.id")
            ->field('t3.id,t3.hx_sjhxje')
            ->where($where)
            ->sum('t3.hx_sjhxje');
        $this->assign('sjhxjehj',$sjhxjehj);
        return $this->fetch();
    }

    //导出excel--非常规和进场
    public function excel(){
        $where = [];
        $sn = $this->request->get('hxmdbh');
        $this->assign('hxmdbh',$sn);
        $jxs_title = $this->request->get('jxsmc');
        $this->assign('jxsmc',$jxs_title);
        $wdmc = $this->request->get('wdmc');
        $this->assign('wdmc',$wdmc);
        $pp = $this->request->get('pp');
        $this->assign('pp',$pp);
        $sjfykm = $this->request->get('sjfykm');
        $this->assign('sjfykm',$sjfykm);
        $sqr = $this->request->get('sqr');
        $this->assign('sqr',$sqr);
        $start = $this->request->get('start');
        $this->assign('start',$start);
        $end = $this->request->get('end');
        $this->assign('end',$end);
        $zffs = $this->request->get('zffs');
        $this->assign('zffs',$zffs);

        if($sn){
            $where['t.sn'] = ['like',"%{$sn}%"];
        }
        if($start&&$end){
            $where['t.ctime'] = ['between',[strtotime($start),strtotime($end)]];
        }elseif($start){
            $where['t.ctime'] = ['>',strtotime($start)];
        }elseif($end){
            $where['t.ctime'] = ['<',strtotime($end)];
        }
        if($jxs_title){
            $jxs_ids = db('cg_jxs')->where('title','like',"%{$jxs_title}%")->column('id');
            $where['t.jxs_id'] = ['in',$jxs_ids];
        }
        if($wdmc){
            $net_ids = db('cg_net')->where('title','like',"%{$wdmc}%")->column('id');
            $where['t3.net_id'] = ['in',$net_ids];
        }
        if($pp){
            $brand_ids = db('pro_brands')->where('brand_name','like',"%{$pp}%")->column('id');
            $where['brand'] = ['in',$brand_ids];
        }
        if($sjfykm){
            $feetypeids = db('cg_feiyongkemu3')->where('title','like',"%{$sjfykm}%")->column('id');
            $where['t3.feetype'] = ['in',$feetypeids];
        }
        if($sqr){
            $sqr_ids = db('employee')->where('em_name','like',"%{$sqr}%")->column('id');
            $where['t.cuser'] = ['in',$sqr_ids];
        }
        if($zffs){
            $zffsids = db('cg_paymethod')->where('title','like',"%{$zffs}%")->column('id');
            $where['t3.paymethod'] = ['in',$zffsids];
        }

        $data = Db::table('cg_ti_hexiao t')
            ->join('cg_ti_hexiao_list t2','t2.hx_id=t.id')
            ->join('cg_ti_list t3','t3.id=t2.list_id')
            ->join('cg_ti t4','t4.id=t3.ti_id')
            ->order("t.id desc")
            ->where($where)
            ->field('t.cuser sqr,t.sn hxmdbh,t.id,t2.sn hxmxbh,t4.type_id,t4.sn ysqdh,t4.id ysqdh_id,t3.sn ysqmxbh,t.jxs_id,t3.net_id,t3.brand,t3.category,t3.series,t3.sku,t3.product,t3.tm_id,t3.feetype,t3.paymethod,t3.fb_shijitourufeiyong sjtrje,t3.hx_yihexiao2 yhxje,t2.weishuihexiaojine wshxje,t2.youwufapiao ywfp,t2.fapiaohao fph,t2.shizengzhishui szzs,t2.shuilv,t2.shuijin,t2.hanshuihexiaojine hshxje,t2.shijitourujine sjtrje,t2.huodongkaishiriqi hdksrq,t2.gdrq,t2.beizhu')
            ->select();
//        echo Db::table('cg_ti')->getLastSql();
        foreach($data as $k=>$v){
            $v['sqr'] = db('employee')->where('id',$v['sqr'])->value('em_name');
            $v['type_title'] = db('cg_ti_type')->where('id',$v['type_id'])->value('title');
            $v['jxs_title'] = db('cg_jxs')->where('id',$v['jxs_id'])->value('title');
            $v['system_title'] = db('cg_system t')->join('cg_net t2','t2.system_id = t.id')->where('t2.id',$v['net_id'])->value('t.title');
            $v['net_title'] = db('cg_net')->where('id',$v['net_id'])->value('title');
            $v['brand_name'] = db('pro_brands')->where('id',$v['brand'])->value('brand_name');
            $v['category_name'] = db('pro_category')->where('id',$v['category'])->value('category_name');
            $v['series_name'] = db('pro_series')->where('id',$v['series'])->value('series_name');
            $v['sku_name'] = db('pro_sku')->where('id',$v['sku'])->value('sku_name');
            if($v['product'])
                $v['pro_name'] = db('pro_products')->where('id',$v['product'])->value('pro_name');
            else
                $v['pro_name'] = db('pro_products')->where('id',$v['tm_id'])->value('pro_name');
            $v['sjkm'] = db('cg_feiyongkemu3')->where('id',$v['feetype'])->value('title');
            $v['ejkm'] = db('cg_feiyongkemu2 t')->join('cg_feiyongkemu3 t2','t2.pid=t.id')->where('t2.id',$v['feetype'])->value('t.title');
            $v['yjkm'] = db('cg_feiyongkemu1 t')->join('cg_feiyongkemu2 t2','t2.pid=t.id')->join('cg_feiyongkemu3 t3','t3.pid=t2.id')->where('t3.id',$v['feetype'])->value('t.title');
            $v['zffs'] = db('cg_paymethod')->where('id',$v['paymethod'])->value('title');
            //反馈完成之后才能显示核销按钮
            $data[$k] = $v;
        }
        //批复金额合计
        $pfjehj = Db::table('cg_ti_hexiao t')
            ->join('cg_ti_hexiao_list t2','t2.hx_id=t.id')
            ->join('cg_ti_list t3','t3.id=t2.list_id')
            ->join('cg_ti t4','t4.id=t3.ti_id')
            ->group("t3.id")
            ->field('t3.id,t3.pizhunhanshui')
            ->where($where)
            ->sum('t3.pizhunhanshui');
        $wshxjehj = Db::table('cg_ti_hexiao t')
            ->join('cg_ti_hexiao_list t2','t2.hx_id=t.id')
            ->join('cg_ti_list t3','t3.id=t2.list_id')
            ->join('cg_ti t4','t4.id=t3.ti_id')
            ->group("t3.id")
            ->field('t3.id,t3.hx_yihexiao')
            ->where($where)
            ->sum('t3.hx_yihexiao');
        $hshxjehj = Db::table('cg_ti_hexiao t')
            ->join('cg_ti_hexiao_list t2','t2.hx_id=t.id')
            ->join('cg_ti_list t3','t3.id=t2.list_id')
            ->join('cg_ti t4','t4.id=t3.ti_id')
            ->group("t3.id")
            ->field('t3.id,t3.hx_yihexiao2')
            ->where($where)
            ->sum('t3.hx_yihexiao2');
        //税金合计
        $sjhj  = $hshxjehj-$wshxjehj;
        //未核销金额合计
        $whxjehj = $pfjehj-$hshxjehj;
        //实际核销金额合计
        $sjhxjehj = Db::table('cg_ti_hexiao t')
            ->join('cg_ti_hexiao_list t2','t2.hx_id=t.id')
            ->join('cg_ti_list t3','t3.id=t2.list_id')
            ->join('cg_ti t4','t4.id=t3.ti_id')
            ->group("t3.id")
            ->field('t3.id,t3.hx_sjhxje')
            ->where($where)
            ->sum('t3.hx_sjhxje');

        Vendor("PHPExcel");
        Vendor("PHPExcel.Writer.Excel5");
        $objPHPExcel= new PHPExcel();
        $objProps=$objPHPExcel->getProperties();

        $objProps->setCreator('admin');
        $objProps->setLastModifiedBy('admin');
        $objProps->setTitle('Office 2003 XLSX Test Document');
        $objProps->setSubject('Office 2003 XLSX Test Document');
        $objProps->setDescription("Test document, generated by PHPExcel.");
        $objProps->setKeywords("office excel PHPExcel");
        $objProps->setCategory("Test");

        $objPHPExcel->setActiveSheetIndex(0);
        $as =  $objPHPExcel->getActiveSheet();
        $as->setTitle("TI费用已核销明细");
        $styleArray = array(
            'borders' => array(
                'allborders' => array(
                    //'style' => PHPExcel_Style_Border::BORDER_THICK,//边框是粗的
                    'style' => PHPExcel_Style_Border::BORDER_THIN,//细边框
                    'color' => array('argb' => ''),
                ),
            ),
        );
        $as->mergeCells('A1:AG1');
        $as->setCellValue('A1','TI费用已核销明细');
        //加粗
        $as->getStyle('A1')->getFont()->setBold(true);
        //字体
        $as->getStyle('A1')->getFont()->setSize(14);
        $as->mergeCells('A2:AG2');
        $username = db('employee')->where('id',session('employee_id'))->value('em_name');
        $as->setCellValue('A2','注：本表单由软件导出，导出时间：'.date('Y-m-d H:i').'导出人：'.$username);
        $as->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $as->getStyle('A2')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);

        //总计
        $as->mergeCells('A3:AG3');
        $as->setCellValue('A3','批复金额合计：'.$pfjehj.'未核销金额合计：'.$whxjehj.'为税核销金额合计：'.$wshxjehj.'税金核销合计：'.$sjhj.'含税核销金额合计：'.$hshxjehj.'实际核销金额合计：'.$sjhxjehj);

        //标题
        $as->getStyle('A4:AG5')->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID);
        $as->getStyle('A4:AG5')->getFill()->getStartColor()->setARGB('0099CCFF');
        $as->mergeCells('A4:A5');
        $as->setCellValue('A4','序号');
        $as->mergeCells('B4:B5');
        $as->setCellValue('B4','申请人');
        $as->mergeCells('C4:C5');
        $as->setCellValue('C4','核销面单编号');
        $as->mergeCells('D4:D5');
        $as->setCellValue('D4','核销明细编号');
        $as->mergeCells('E4:E5');
        $as->setCellValue('E4','申请类型');
        $as->mergeCells('F4:F5');
        $as->setCellValue('F4','原申请编号');
        $as->mergeCells('G4:G5');
        $as->setCellValue('G4','原申请明细编号');
        $as->mergeCells('H4:H5');
        $as->setCellValue('H4','经销商名');
        $as->mergeCells('I4:I5');
        $as->setCellValue('I4','系统名称');
        $as->mergeCells('J4:J5');
        $as->setCellValue('J4','网点名称');
        $as->mergeCells('K4:K5');
        $as->setCellValue('K4','品牌名称');
        $as->mergeCells('L4:L5');
        $as->setCellValue('L4','品类名称');
        $as->mergeCells('M4:M5');
        $as->setCellValue('M4','系列名称');
        $as->mergeCells('N4:N5');
        $as->setCellValue('N4','SKU');
        $as->mergeCells('O4:O5');
        $as->setCellValue('O4','单品');
        $as->mergeCells('P4:P5');
        $as->setCellValue('P4','一级科目');
        $as->mergeCells('Q4:Q5');
        $as->setCellValue('Q4','二级科目');
        $as->mergeCells('R4:R5');
        $as->setCellValue('R4','三级科目');
        $as->mergeCells('S4:S5');
        $as->setCellValue('S4','支付方式');
        $as->mergeCells('T4:V4');
        $as->setCellValue('T4','实际投入');
        $as->getStyle('T4')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $as->setCellValue('T5','实际投入金额');
        $as->setCellValue('U5','已核销金额');
        $as->setCellValue('V5','未核销金额');
        $as->mergeCells('W4:AD4');
        $as->setCellValue('W4','本次申请核销');
        $as->getStyle('W4')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $as->setCellValue('W5','未税核销金额');
        $as->setCellValue('X5','有无发票');
        $as->setCellValue('Y5','发票号');
        $as->setCellValue('Z5','是否增值税（可抵扣）');
        $as->setCellValue('AA5','税率');
        $as->setCellValue('AB5','税金');
        $as->setCellValue('AC5','含税核销金额');
        $as->setCellValue('AD5','实际核销金额');
        $as->mergeCells('AE4:AE5');
        $as->setCellValue('AE4','活动开始日期');
        $as->mergeCells('AF4:AF5');
        $as->setCellValue('AF4','归档日期');
        $as->mergeCells('AG4:AG5');
        $as->setCellValue('AG4','备注');

        foreach($data as $k=>$v){
            $n = 6+$k;
            $as->setCellValue('A'.$n,$k+1);
            $as->setCellValue('B'.$n,''.$v['sqr']);
            $as->setCellValue('C'.$n,''.$v['hxmdbh']);
            $as->setCellValue('D'.$n,$v['hxmdbh'].'-'.$v['hxmxbh']);
            $as->setCellValue('E'.$n,$v['type_title']);
            $as->setCellValue('F'.$n,$v['ysqdh']);
            $as->setCellValue('G'.$n,$v['ysqdh'].'-'.$v['ysqmxbh']);
            $as->setCellValue('H'.$n,$v['jxs_title']);
            $as->setCellValue('I'.$n,$v['system_title']);
            $as->setCellValue('J'.$n,$v['net_title']);
            $as->setCellValue('K'.$n,$v['brand_name']);
            $as->setCellValue('L'.$n,$v['category_name']);
            $as->setCellValue('M'.$n,$v['series_name']);
            $as->setCellValue('N'.$n,$v['sku_name']);
            $as->setCellValue('O'.$n,$v['pro_name']);
            $as->setCellValue('P'.$n,$v['yjkm']);
            $as->setCellValue('Q'.$n,$v['ejkm']);
            $as->setCellValue('R'.$n,$v['zffs']);
            $as->setCellValue('S'.$n,$v['sjtrje']);
            $as->setCellValue('T'.$n,$v['yhxje']);
            $as->setCellValue('U'.$n,$v['sjtrje']-$v['yhxje']);
            $as->setCellValue('V'.$n,$v['wshxje']);
            $as->setCellValue('W'.$n,$v['ywfp']?'有':'无');
            $as->setCellValue('X'.$n,$v['fph']?$v['fph']:'-');
            $as->setCellValue('Y'.$n,$v['szzs']?'是':'否');
            $as->setCellValue('Z'.$n,$v['shuilv']);
            $as->setCellValue('AA'.$n,$v['shuijin']);
            $as->setCellValue('AB'.$n,$v['hshxje']);
            $as->setCellValue('AC'.$n,$v['sjtrje']);
            $as->setCellValue('AD'.$n,date('Y-m-d',$v['hdksrq']));
            $as->setCellValue('AE'.$n,$v['gdrq']);
            $as->setCellValue('AF'.$n,$v['beizhu']);
        }

        $n = $as->getHighestRow();
        $as->getStyle('A1:AG'.$n)->applyFromArray($styleArray);
        $objWriter = new PHPExcel_Writer_Excel5($objPHPExcel);
        ob_end_clean();
        header("Pragma: public");
        header("Expires:0");
        header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
        header("Content-Type:application/force-download");
        header("Content-Type:application/vnd.ms-execl");
        header("Content-Type:application/octet-stream");
        header("Content-Type:application/download");
        header('Content-Disposition:attachment;filename=TI费用已核销明细查询-非常规和进场.xls');
        header("Content-type:text/html;charset=utf-8");
        header("Content-Transfer-Encoding:binary");
        $objWriter->save('php://output');

    }

    //列表--常规
    public function cgindex()
    {
        $where = [];
        $sn = $this->request->get('hxmdbh');
        $this->assign('hxmdbh',$sn);
        $jxs_title = $this->request->get('jxsmc');
        $this->assign('jxsmc',$jxs_title);
        $wdmc = $this->request->get('wdmc');
        $this->assign('wdmc',$wdmc);
        $pp = $this->request->get('pp');
        $this->assign('pp',$pp);
        $sjfykm = $this->request->get('sjfykm');
        $this->assign('sjfykm',$sjfykm);
        $sqr = $this->request->get('sqr');
        $this->assign('sqr',$sqr);
        $start = $this->request->get('start');
        $this->assign('start',$start);
        $end = $this->request->get('end');
        $this->assign('end',$end);
        $zffs = $this->request->get('zffs');
        $this->assign('zffs',$zffs);

        if($sn){
            $where['t.no'] = ['like',"%{$sn}%"];
        }
        if($start&&$end){
            $where['t.created_on'] = ['between',[strtotime($start),strtotime($end)]];
        }elseif($start){
            $where['t.created_on'] = ['>',strtotime($start)];
        }elseif($end){
            $where['t.created_on'] = ['<',strtotime($end)];
        }
        if($jxs_title){
            $jxs_ids = db('cg_jxs')->where('title','like',"%{$jxs_title}%")->column('id');
            $where['t.jxs_id'] = ['in',$jxs_ids];
        }
        if($wdmc){
            $net_ids = db('cg_net')->where('title','like',"%{$wdmc}%")->column('id');
            $where['t3.net_id'] = ['in',$net_ids];
        }
        if($pp){
            $brand_ids = db('pro_products t')
                ->join('pro_sku t2','t2.id=t.sku_id')
                ->join('pro_series t3','t3.id=t2.series_id')
                ->join('pro_category t4','t4.id=t3.category_id')
                ->join('pro_brands t5','t5.id=t4.brand_id')
                ->where('brand_name','like',"%{$pp}%")
                ->column('t.id');
            $where['pro_id'] = ['in',$brand_ids];
        }
        if($sjfykm){
            $feetypeids = db('cg_feiyongkemu3')->where('title','like',"%{$sjfykm}%")->column('id');
            $where['t3.feiyong_id'] = ['in',$feetypeids];
        }
        if($sqr){
            $sqr_ids = db('employee')->where('em_name','like',"%{$sqr}%")->column('id');
            $where['t.c_user_id'] = ['in',$sqr_ids];
        }
        if($zffs){
            $zffsids = db('cg_paymethod')->where('title','like',"%{$zffs}%")->column('id');
            $where['t3.zfid'] = ['in',$zffsids];
        }

        $count = Db::table('ti_hexiao_apply t')
            ->join('ti_hexiao_detail t2','t2.hexiao_id=t.id')
            ->join('ti_apply_detail t3','t3.id=t2.detail_id')
            ->join('ti_apply t4','t3.apply_id=t4.id')
            ->where($where)->count();
        $data = Db::table('ti_hexiao_apply t')
            ->join('ti_hexiao_detail t2','t2.hexiao_id=t.id')
            ->join('ti_apply_detail t3','t3.id=t2.detail_id')
            ->join('ti_apply t4','t3.apply_id=t4.id')
            ->order("t.id desc")
            ->where($where)
            ->field('t.c_user_id sqr,t.no hxmdbh,t.id,t2.no hxmxbh,t4.no ysqdh,t4.id ysqdh_id,t3.detail_no ysqmxbh,t.jxs_id,t3.net_id,t3.pro_id product,t3.feiyong_id feetype,t3.zfid paymethod,t3.fankuijine sjtrje,t3.hx_yihexiao2 yhxje,t2.hexiao_weishui wshxje,t2.has_fp ywfp,t2.fp_no fph,t2.dikou szzs,t2.hexiao_shuilv shuilv,t2.hdksrq,t2.gdrq,t2.beizhu')
            ->paginate(10,$count);
//        echo Db::table('cg_ti')->getLastSql();
        $page = $data->render();
        $data = $data->items();
        foreach($data as $k=>$v){
            $v['shuijin'] = round($v['wshxje']*($v['shuilv']/100),2);
            $v['hshxje'] = round($v['wshxje']*(1+$v['shuilv']/100),2);
            if($v['dikou'])
                $v['sjtrje'] = $v['wshxje'];
            else
                $v['sjtrje'] = $v['hshxje'];
            $v['sqr'] = db('employee')->where('id',$v['sqr'])->value('em_name');
            $v['type_title'] = db('cg_ti_type')->where('id',$v['type_id'])->value('title');
            $v['jxs_title'] = db('cg_jxs')->where('id',$v['jxs_id'])->value('title');
            $v['system_title'] = db('cg_system t')->join('cg_net t2','t2.system_id = t.id')->where('t2.id',$v['net_id'])->value('t.title');
            $v['net_title'] = db('cg_net')->where('id',$v['net_id'])->value('title');
            $v['brand_name'] = db('pro_brands')->where('id',$v['brand'])->value('brand_name');
            $v['category_name'] = db('pro_category')->where('id',$v['category'])->value('category_name');
            $v['series_name'] = db('pro_series')->where('id',$v['series'])->value('series_name');
            $v['sku_name'] = db('pro_sku')->where('id',$v['sku'])->value('sku_name');
            if($v['product'])
                $v['pro_name'] = db('pro_products')->where('id',$v['product'])->value('pro_name');
            else
                $v['pro_name'] = db('pro_products')->where('id',$v['tm_id'])->value('pro_name');
            $v['sjkm'] = db('cg_feiyongkemu3')->where('id',$v['feetype'])->value('title');
            $v['ejkm'] = db('cg_feiyongkemu2 t')->join('cg_feiyongkemu3 t2','t2.pid=t.id')->where('t2.id',$v['feetype'])->value('t.title');
            $v['yjkm'] = db('cg_feiyongkemu1 t')->join('cg_feiyongkemu2 t2','t2.pid=t.id')->join('cg_feiyongkemu3 t3','t3.pid=t2.id')->where('t3.id',$v['feetype'])->value('t.title');
            $v['zffs'] = db('cg_paymethod')->where('id',$v['paymethod'])->value('title');
            $data[$k] = $v;
        }

        $this->assign('data',$data);
        $this->assign('page',$page);
        //批复金额合计-含税
        $pfjehj = Db::table('ti_hexiao_apply t')
            ->join('ti_hexiao_detail t2','t2.hexiao_id=t.id')
            ->join('ti_apply_detail t3','t3.id=t2.detail_id')
            ->join('ti_apply t4','t3.apply_id=t4.id')
            ->group("t3.id")
            ->field('t3.id,t3.pifuzonge*(1+t3.shuilv/100)')
            ->where($where)
            ->sum('t3.pifuzonge*(1+t3.shuilv/100)');
        $pfjehj = round($pfjehj,2);
        $this->assign('pfjehj',$pfjehj);
        $wshxjehj = Db::table('ti_hexiao_apply t')
            ->join('ti_hexiao_detail t2','t2.hexiao_id=t.id')
            ->join('ti_apply_detail t3','t3.id=t2.detail_id')
            ->join('ti_apply t4','t3.apply_id=t4.id')
            ->group("t3.id")
            ->field('t3.id,t3.hx_yihexiao')
            ->where($where)
            ->sum('t3.hx_yihexiao');
        $this->assign('wshxjehj',$wshxjehj); //未税核销金额合计
        $hshxjehj = Db::table('ti_hexiao_apply t')
            ->join('ti_hexiao_detail t2','t2.hexiao_id=t.id')
            ->join('ti_apply_detail t3','t3.id=t2.detail_id')
            ->join('ti_apply t4','t3.apply_id=t4.id')
            ->group("t3.id")
            ->field('t3.id,t3.hx_yihexiao2')
            ->where($where)
            ->sum('t3.hx_yihexiao2');
        $this->assign('hshxjehj',$hshxjehj); //含税核销金额合计
        //税金合计
        $sjhj  = $hshxjehj-$wshxjehj;
        $this->assign('sjhj',$sjhj);
        //未核销金额合计
        $whxjehj = $pfjehj-$hshxjehj;
        $this->assign('whxjehj',$whxjehj);
        //实际核销金额合计
        $sjhxjehj = Db::table('ti_hexiao_apply t')
            ->join('ti_hexiao_detail t2','t2.hexiao_id=t.id')
            ->join('ti_apply_detail t3','t3.id=t2.detail_id')
            ->join('ti_apply t4','t3.apply_id=t4.id')
            ->group("t3.id")
            ->field('t3.id,t3.hx_sjhxje')
            ->where($where)
            ->sum('t3.hx_sjhxje');
        $this->assign('sjhxjehj',$sjhxjehj);
        return $this->fetch();
    }

    //导出excel--常规
    public function cgexcel(){
        $where = [];
        $sn = $this->request->get('hxmdbh');
        $this->assign('hxmdbh',$sn);
        $jxs_title = $this->request->get('jxsmc');
        $this->assign('jxsmc',$jxs_title);
        $wdmc = $this->request->get('wdmc');
        $this->assign('wdmc',$wdmc);
        $pp = $this->request->get('pp');
        $this->assign('pp',$pp);
        $sjfykm = $this->request->get('sjfykm');
        $this->assign('sjfykm',$sjfykm);
        $sqr = $this->request->get('sqr');
        $this->assign('sqr',$sqr);
        $start = $this->request->get('start');
        $this->assign('start',$start);
        $end = $this->request->get('end');
        $this->assign('end',$end);
        $zffs = $this->request->get('zffs');
        $this->assign('zffs',$zffs);

        if($sn){
            $where['t.no'] = ['like',"%{$sn}%"];
        }
        if($start&&$end){
            $where['t.created_on'] = ['between',[strtotime($start),strtotime($end)]];
        }elseif($start){
            $where['t.created_on'] = ['>',strtotime($start)];
        }elseif($end){
            $where['t.created_on'] = ['<',strtotime($end)];
        }
        if($jxs_title){
            $jxs_ids = db('cg_jxs')->where('title','like',"%{$jxs_title}%")->column('id');
            $where['t.jxs_id'] = ['in',$jxs_ids];
        }
        if($wdmc){
            $net_ids = db('cg_net')->where('title','like',"%{$wdmc}%")->column('id');
            $where['t3.net_id'] = ['in',$net_ids];
        }
        if($pp){
            $brand_ids = db('pro_products t')
                ->join('pro_sku t2','t2.id=t.sku_id')
                ->join('pro_series t3','t3.id=t2.series_id')
                ->join('pro_category t4','t4.id=t3.category_id')
                ->join('pro_brands t5','t5.id=t4.brand_id')
                ->where('brand_name','like',"%{$pp}%")
                ->column('t.id');
            $where['pro_id'] = ['in',$brand_ids];
        }
        if($sjfykm){
            $feetypeids = db('cg_feiyongkemu3')->where('title','like',"%{$sjfykm}%")->column('id');
            $where['t3.feiyong_id'] = ['in',$feetypeids];
        }
        if($sqr){
            $sqr_ids = db('employee')->where('em_name','like',"%{$sqr}%")->column('id');
            $where['t.c_user_id'] = ['in',$sqr_ids];
        }
        if($zffs){
            $zffsids = db('cg_paymethod')->where('title','like',"%{$zffs}%")->column('id');
            $where['t3.zfid'] = ['in',$zffsids];
        }

        $data = Db::table('ti_hexiao_apply t')
            ->join('ti_hexiao_detail t2','t2.hexiao_id=t.id')
            ->join('ti_apply_detail t3','t3.id=t2.detail_id')
            ->join('ti_apply t4','t3.apply_id=t4.id')
            ->order("t.id desc")
            ->where($where)
            ->field('t.c_user_id sqr,t.no hxmdbh,t.id,t2.no hxmxbh,t4.no ysqdh,t4.id ysqdh_id,t3.detail_no ysqmxbh,t.jxs_id,t3.net_id,t3.pro_id product,t3.feiyong_id feetype,t3.zfid paymethod,t3.fankuijine sjtrje,t3.hx_yihexiao2 yhxje,t2.hexiao_weishui wshxje,t2.has_fp ywfp,t2.fp_no fph,t2.dikou szzs,t2.hexiao_shuilv shuilv,t2.hdksrq,t2.gdrq,t2.beizhu')
            ->select();
        foreach($data as $k=>$v){
            $v['shuijin'] = round($v['wshxje']*($v['shuilv']/100),2);
            $v['hshxje'] = round($v['wshxje']*(1+$v['shuilv']/100),2);
            if($v['dikou'])
                $v['sjtrje'] = $v['wshxje'];
            else
                $v['sjtrje'] = $v['hshxje'];
            $v['sqr'] = db('employee')->where('id',$v['sqr'])->value('em_name');
            $v['type_title'] = db('cg_ti_type')->where('id',$v['type_id'])->value('title');
            $v['jxs_title'] = db('cg_jxs')->where('id',$v['jxs_id'])->value('title');
            $v['system_title'] = db('cg_system t')->join('cg_net t2','t2.system_id = t.id')->where('t2.id',$v['net_id'])->value('t.title');
            $v['net_title'] = db('cg_net')->where('id',$v['net_id'])->value('title');
            $v['brand_name'] = db('pro_brands')->where('id',$v['brand'])->value('brand_name');
            $v['category_name'] = db('pro_category')->where('id',$v['category'])->value('category_name');
            $v['series_name'] = db('pro_series')->where('id',$v['series'])->value('series_name');
            $v['sku_name'] = db('pro_sku')->where('id',$v['sku'])->value('sku_name');
            if($v['product'])
                $v['pro_name'] = db('pro_products')->where('id',$v['product'])->value('pro_name');
            else
                $v['pro_name'] = db('pro_products')->where('id',$v['tm_id'])->value('pro_name');
            $v['sjkm'] = db('cg_feiyongkemu3')->where('id',$v['feetype'])->value('title');
            $v['ejkm'] = db('cg_feiyongkemu2 t')->join('cg_feiyongkemu3 t2','t2.pid=t.id')->where('t2.id',$v['feetype'])->value('t.title');
            $v['yjkm'] = db('cg_feiyongkemu1 t')->join('cg_feiyongkemu2 t2','t2.pid=t.id')->join('cg_feiyongkemu3 t3','t3.pid=t2.id')->where('t3.id',$v['feetype'])->value('t.title');
            $v['zffs'] = db('cg_paymethod')->where('id',$v['paymethod'])->value('title');
            $data[$k] = $v;
        }

        //批复金额合计-含税
        $pfjehj = Db::table('ti_hexiao_apply t')
            ->join('ti_hexiao_detail t2','t2.hexiao_id=t.id')
            ->join('ti_apply_detail t3','t3.id=t2.detail_id')
            ->join('ti_apply t4','t3.apply_id=t4.id')
            ->group("t3.id")
            ->field('t3.id,t3.pifuzonge*(1+t3.shuilv/100)')
            ->where($where)
            ->sum('t3.pifuzonge*(1+t3.shuilv/100)');
        $pfjehj = round($pfjehj,2);
        $wshxjehj = Db::table('ti_hexiao_apply t')
            ->join('ti_hexiao_detail t2','t2.hexiao_id=t.id')
            ->join('ti_apply_detail t3','t3.id=t2.detail_id')
            ->join('ti_apply t4','t3.apply_id=t4.id')
            ->group("t3.id")
            ->field('t3.id,t3.hx_yihexiao')
            ->where($where)
            ->sum('t3.hx_yihexiao');
        $hshxjehj = Db::table('ti_hexiao_apply t')
            ->join('ti_hexiao_detail t2','t2.hexiao_id=t.id')
            ->join('ti_apply_detail t3','t3.id=t2.detail_id')
            ->join('ti_apply t4','t3.apply_id=t4.id')
            ->group("t3.id")
            ->field('t3.id,t3.hx_yihexiao2')
            ->where($where)
            ->sum('t3.hx_yihexiao2');
        //税金合计
        $sjhj  = $hshxjehj-$wshxjehj;
        //未核销金额合计
        $whxjehj = $pfjehj-$hshxjehj;
        //实际核销金额合计
        $sjhxjehj = Db::table('ti_hexiao_apply t')
            ->join('ti_hexiao_detail t2','t2.hexiao_id=t.id')
            ->join('ti_apply_detail t3','t3.id=t2.detail_id')
            ->join('ti_apply t4','t3.apply_id=t4.id')
            ->group("t3.id")
            ->field('t3.id,t3.hx_sjhxje')
            ->where($where)
            ->sum('t3.hx_sjhxje');

        Vendor("PHPExcel");
        Vendor("PHPExcel.Writer.Excel5");
        $objPHPExcel= new PHPExcel();
        $objProps=$objPHPExcel->getProperties();

        $objProps->setCreator('admin');
        $objProps->setLastModifiedBy('admin');
        $objProps->setTitle('Office 2003 XLSX Test Document');
        $objProps->setSubject('Office 2003 XLSX Test Document');
        $objProps->setDescription("Test document, generated by PHPExcel.");
        $objProps->setKeywords("office excel PHPExcel");
        $objProps->setCategory("Test");

        $objPHPExcel->setActiveSheetIndex(0);
        $as =  $objPHPExcel->getActiveSheet();
        $as->setTitle("TI费用已核销明细-常规");
        $styleArray = array(
            'borders' => array(
                'allborders' => array(
                    //'style' => PHPExcel_Style_Border::BORDER_THICK,//边框是粗的
                    'style' => PHPExcel_Style_Border::BORDER_THIN,//细边框
                    'color' => array('argb' => ''),
                ),
            ),
        );
        $as->mergeCells('A1:AG1');
        $as->setCellValue('A1','TI费用已核销明细-常规');
        //加粗
        $as->getStyle('A1')->getFont()->setBold(true);
        //字体
        $as->getStyle('A1')->getFont()->setSize(14);
        $as->mergeCells('A2:AG2');
        $username = db('employee')->where('id',session('employee_id'))->value('em_name');
        $as->setCellValue('A2','注：本表单由软件导出，导出时间：'.date('Y-m-d H:i').'导出人：'.$username);
        $as->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $as->getStyle('A2')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);

        //总计
        $as->mergeCells('A3:AG3');
        $as->setCellValue('A3','批复金额合计：'.$pfjehj.'未核销金额合计：'.$whxjehj.'为税核销金额合计：'.$wshxjehj.'税金核销合计：'.$sjhj.'含税核销金额合计：'.$hshxjehj.'实际核销金额合计：'.$sjhxjehj);

        //标题
        $as->getStyle('A4:AG5')->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID);
        $as->getStyle('A4:AG5')->getFill()->getStartColor()->setARGB('0099CCFF');
        $as->mergeCells('A4:A5');
        $as->setCellValue('A4','序号');
        $as->mergeCells('B4:B5');
        $as->setCellValue('B4','申请人');
        $as->mergeCells('C4:C5');
        $as->setCellValue('C4','核销面单编号');
        $as->mergeCells('D4:D5');
        $as->setCellValue('D4','核销明细编号');
        $as->mergeCells('E4:E5');
        $as->setCellValue('E4','申请类型');
        $as->mergeCells('F4:F5');
        $as->setCellValue('F4','原申请编号');
        $as->mergeCells('G4:G5');
        $as->setCellValue('G4','原申请明细编号');
        $as->mergeCells('H4:H5');
        $as->setCellValue('H4','经销商名');
        $as->mergeCells('I4:I5');
        $as->setCellValue('I4','系统名称');
        $as->mergeCells('J4:J5');
        $as->setCellValue('J4','网点名称');
        $as->mergeCells('K4:K5');
        $as->setCellValue('K4','品牌名称');
        $as->mergeCells('L4:L5');
        $as->setCellValue('L4','品类名称');
        $as->mergeCells('M4:M5');
        $as->setCellValue('M4','系列名称');
        $as->mergeCells('N4:N5');
        $as->setCellValue('N4','SKU');
        $as->mergeCells('O4:O5');
        $as->setCellValue('O4','单品');
        $as->mergeCells('P4:P5');
        $as->setCellValue('P4','一级科目');
        $as->mergeCells('Q4:Q5');
        $as->setCellValue('Q4','二级科目');
        $as->mergeCells('R4:R5');
        $as->setCellValue('R4','三级科目');
        $as->mergeCells('S4:S5');
        $as->setCellValue('S4','支付方式');
        $as->mergeCells('T4:V4');
        $as->setCellValue('T4','实际投入');
        $as->getStyle('T4')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $as->setCellValue('T5','实际投入金额');
        $as->setCellValue('U5','已核销金额');
        $as->setCellValue('V5','未核销金额');
        $as->mergeCells('W4:AD4');
        $as->setCellValue('W4','本次申请核销');
        $as->getStyle('W4')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $as->setCellValue('W5','未税核销金额');
        $as->setCellValue('X5','有无发票');
        $as->setCellValue('Y5','发票号');
        $as->setCellValue('Z5','是否增值税（可抵扣）');
        $as->setCellValue('AA5','税率');
        $as->setCellValue('AB5','税金');
        $as->setCellValue('AC5','含税核销金额');
        $as->setCellValue('AD5','实际核销金额');
        $as->mergeCells('AE4:AE5');
        $as->setCellValue('AE4','活动开始日期');
        $as->mergeCells('AF4:AF5');
        $as->setCellValue('AF4','归档日期');
        $as->mergeCells('AG4:AG5');
        $as->setCellValue('AG4','备注');

        foreach($data as $k=>$v){
            $n = 6+$k;
            $as->setCellValue('A'.$n,$k+1);
            $as->setCellValue('B'.$n,''.$v['sqr']);
            $as->setCellValue('C'.$n,''.$v['hxmdbh']);
            $as->setCellValue('D'.$n,$v['hxmdbh'].'-'.$v['hxmxbh']);
            $as->setCellValue('E'.$n,$v['type_title']);
            $as->setCellValue('F'.$n,$v['ysqdh']);
            $as->setCellValue('G'.$n,$v['ysqdh'].'-'.$v['ysqmxbh']);
            $as->setCellValue('H'.$n,$v['jxs_title']);
            $as->setCellValue('I'.$n,$v['system_title']);
            $as->setCellValue('J'.$n,$v['net_title']);
            $as->setCellValue('K'.$n,$v['brand_name']);
            $as->setCellValue('L'.$n,$v['category_name']);
            $as->setCellValue('M'.$n,$v['series_name']);
            $as->setCellValue('N'.$n,$v['sku_name']);
            $as->setCellValue('O'.$n,$v['pro_name']);
            $as->setCellValue('P'.$n,$v['yjkm']);
            $as->setCellValue('Q'.$n,$v['ejkm']);
            $as->setCellValue('R'.$n,$v['zffs']);
            $as->setCellValue('S'.$n,$v['sjtrje']);
            $as->setCellValue('T'.$n,$v['yhxje']);
            $as->setCellValue('U'.$n,$v['sjtrje']-$v['yhxje']);
            $as->setCellValue('V'.$n,$v['wshxje']);
            $as->setCellValue('W'.$n,$v['ywfp']?'有':'无');
            $as->setCellValue('X'.$n,$v['fph']?$v['fph']:'-');
            $as->setCellValue('Y'.$n,$v['szzs']?'是':'否');
            $as->setCellValue('Z'.$n,$v['shuilv']);
            $as->setCellValue('AA'.$n,$v['shuijin']);
            $as->setCellValue('AB'.$n,$v['hshxje']);
            $as->setCellValue('AC'.$n,$v['sjtrje']);
            $as->setCellValue('AD'.$n,date('Y-m-d',$v['hdksrq']));
            $as->setCellValue('AE'.$n,$v['gdrq']);
            $as->setCellValue('AF'.$n,$v['beizhu']);
        }

        $n = $as->getHighestRow();
        $as->getStyle('A1:AG'.$n)->applyFromArray($styleArray);
        $objWriter = new PHPExcel_Writer_Excel5($objPHPExcel);
        ob_end_clean();
        header("Pragma: public");
        header("Expires:0");
        header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
        header("Content-Type:application/force-download");
        header("Content-Type:application/vnd.ms-execl");
        header("Content-Type:application/octet-stream");
        header("Content-Type:application/download");
        header('Content-Disposition:attachment;filename=TI费用已核销明细查询-常规.xls');
        header("Content-type:text/html;charset=utf-8");
        header("Content-Transfer-Encoding:binary");
        $objWriter->save('php://output');

    }

    //根据id查找状态
    public function status($id){
        $data = [
            1=>'草稿',
            2=>'已打回',
            3=>'区域领导审批',
            4=>'拆分人审批',
            5=>'集团领导审批',
            6=>'抄送',
            7=>'已归档',
            8=>'已作废',
            9=>'已核销',
        ];
        return $data[$id];
    }

    //返回二维数组中的某二维key的合计
    public function array_sum($arr,$key){
        $sum = 0;
        foreach($arr as $k=>$v){
            $sum += $v[$key];
        }
        return $sum;
    }
}
